//
//  ViewController.m
//  A One POS
//
//  Created by AonePOS on 24/09/13.
//  Copyright (c) 2013 AonePOS. All rights reserved.

#import "ViewController.h"
#import "general data.h"
#import "DataBaseManager.h"
#import "MainView.h"
//#import "NSString+md5string.h"

#define SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(v)  ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedAscending)
@interface ViewController ()

@end

@implementation ViewController
{
    general_data *generalDataObject ;
}
@synthesize theImage;
- (void)viewDidLoad
{
    generalDataObject =[general_data new];
    [generalDataObject  GetuserDataVariablesMethod];
  
   
    [super viewDidLoad];
    isLandScope = UIInterfaceOrientationIsLandscape([UIApplication sharedApplication].statusBarOrientation);
  
    y=0;
    if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"7.0"))
    {
        y=y+20;
    }
    CGFloat startingposition=(self.view.frame.size.width/4);
    CGFloat width,height;
    width=self.view.frame.size.width;
    height=self.view.frame.size.height;
   
    logobackview=[[UIImageView alloc]init];
    logobackview.frame=CGRectMake(0.0, y,width , (height/4));
    [logobackview setImage:[UIImage imageNamed:@"logo-login-bg.png"]];
    [self.view addSubview:logobackview];
    
    logoimageview=[[UIImageView alloc]init];
    if (isLandScope) {
    logoimageview.frame=CGRectMake(startingposition+40, y+20, logobackview.frame.size.width/3, (height/4)-110);
    }
    else
    {
         logoimageview.frame=CGRectMake(startingposition+40, y+20, logobackview.frame.size.width/3,(logobackview.frame.size.width/3)-60);
    }
   
    [logoimageview setImage:[UIImage imageNamed:@"aoneposlogo.png"]];
   
    [logoimageview sizeToFit];
    [logobackview addSubview:logoimageview];
    //NSLog(@"%f",logoimageview.frame.size.height);
    
    if (!backgroundcolorview) {
        
    backgroundcolorview=[[UIView alloc]initWithFrame:CGRectMake(0,(height/4)+y,width,(height-((height/4))))];
    [backgroundcolorview  setBackgroundColor:[UIColor colorWithRed:0.0f green:74.0f/255.0f blue:130.0f/255.0f alpha:1.0f]];
    [self.view addSubview:backgroundcolorview];
    
    
    HomeImageView=[[UIImageView alloc]init];
    if (isLandScope){
        HomeImageView.frame=CGRectMake(10,20,380,340);
    }else{
        HomeImageView.frame=CGRectMake(startingposition,25,400,300);
    }

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *getImagePath = [documentsDirectory stringByAppendingPathComponent:@"savedImage.png"];
    UIImage *img2 = [UIImage imageWithContentsOfFile:getImagePath];

    if (img2) {
        //HomeImageView.backgroundColor = [UIColor colorWithPatternImage:img2];
        [HomeImageView setImage:img2];
    }else{
        //HomeImageView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"aoneposlogo.png"]];
        [HomeImageView setImage:[UIImage imageNamed:@"sereen_logo(small).png"]];
    }
    [backgroundcolorview addSubview:HomeImageView];
    
    img2=nil;
    usernameleftview=[[UIImageView alloc]init];
    if (isLandScope){
        usernameleftview.frame=CGRectMake(startingposition+150,(backgroundcolorview.frame.size.height/3)-20, 70, 80);
    }else{
        usernameleftview.frame=CGRectMake(startingposition,(backgroundcolorview.frame.size.height/3)+100, 70, 80);
    }
    [usernameleftview setImage:[UIImage imageNamed:@"login-username-lefticon.png"]];
    [backgroundcolorview addSubview:usernameleftview];
    
    passwordleftview=[[UIImageView alloc]init];
    if (isLandScope){
        passwordleftview.frame=CGRectMake(startingposition+150,(backgroundcolorview.frame.size.height/3)+70, 70, 80);
    }else{
        passwordleftview.frame=CGRectMake(startingposition,(backgroundcolorview.frame.size.height/3)+90+100, 70, 80);
    }
    UIImage *img=[UIImage imageNamed:@"login-password-lefticon.png"];
    
    [passwordleftview setImage:img];
    img=nil;
    [backgroundcolorview addSubview:passwordleftview];
    
    username_text=[[UITextField alloc]init];
    //username_text.clearButtonMode = UITextFieldViewModeWhileEditing;
    if (isLandScope){
        username_text.frame=CGRectMake(startingposition+usernameleftview.frame.size.width+150, (backgroundcolorview.frame.size.height/4)+10, 300, 80);
    }else{
        username_text.frame=CGRectMake(startingposition+usernameleftview.frame.size.width, (backgroundcolorview.frame.size.height/4)+65+100, 300, 80);
    }
    
    [username_text setBorderStyle:UITextBorderStyleNone];
    [username_text setBackground:[UIImage imageNamed:@"loginfieldbg.png"]];
    username_text.placeholder = @"User Name";
    username_text.textAlignment= NSTextAlignmentLeft;
    username_text.contentVerticalAlignment=UIControlContentVerticalAlignmentCenter;
    UIView *paddingview=[[UIView alloc] initWithFrame:CGRectMake(5, 0, 5, 20)];
    username_text.autocapitalizationType = UITextAutocapitalizationTypeNone;
    username_text.leftView = paddingview;
    username_text.leftViewMode = UITextFieldViewModeAlways;
    username_text.delegate=self;
    [username_text setAutocorrectionType:UITextAutocorrectionTypeNo];
    username_text.font = [UIFont systemFontOfSize:30.0f];
    [backgroundcolorview addSubview:username_text];
    password_text=[[UITextField alloc]init];
    //password_text.clearButtonMode = UITextFieldViewModeWhileEditing;
    if (isLandScope){
        password_text.frame=CGRectMake(startingposition+passwordleftview.frame.size.width+150, (backgroundcolorview.frame.size.height/4)+100, 300, 80);
    }else{
        password_text.frame=CGRectMake(startingposition+passwordleftview.frame.size.width, (backgroundcolorview.frame.size.height/4)+150+100, 300, 80);
    }
    password_text.textAlignment= NSTextAlignmentLeft;
    password_text.contentVerticalAlignment=UIControlContentVerticalAlignmentCenter;
    [password_text setBorderStyle:UITextBorderStyleNone];
    UIView *paddingview2=[[UIView alloc] initWithFrame:CGRectMake(5, 0, 5, 20)];
    password_text.leftView = paddingview2;
    password_text.leftViewMode = UITextFieldViewModeAlways;
    password_text.autocapitalizationType = UITextAutocapitalizationTypeNone;
    [password_text setBackgroundColor:[UIColor clearColor]];
    [password_text setBackground:[UIImage imageNamed:@"password-field-bg.png"]];
    password_text.font = [UIFont systemFontOfSize:30.0f];
    password_text.placeholder = @"Password";
    password_text.delegate=self;
    password_text.secureTextEntry=YES;
    [password_text setAutocorrectionType:UITextAutocorrectionTypeNo];
    [backgroundcolorview addSubview:password_text];
    @autoreleasepool {
        login_button=[UIButton buttonWithType:UIButtonTypeRoundedRect];
        if (isLandScope){
            login_button.frame=CGRectMake(startingposition+passwordleftview.frame.size.width+username_text.frame.size.width-30+150, (backgroundcolorview.frame.size.height/4)+30, 130, 130);
        }else{
            login_button.frame=CGRectMake(startingposition+passwordleftview.frame.size.width+username_text.frame.size.width-30, (backgroundcolorview.frame.size.height/4)+80+100, 130, 130);
        }
        [login_button setBackgroundImage:[UIImage imageNamed:@"login-button-normal.png"] forState:UIControlStateNormal];
        [login_button addTarget:self action:@selector(loginClicked) forControlEvents:UIControlEventTouchUpInside];
        [backgroundcolorview addSubview:login_button];
    }
  
    
    sationLbl=[[UILabel alloc] init];
    if (isLandScope){
        sationLbl.frame=CGRectMake(backgroundcolorview.frame.size.width-200, backgroundcolorview.frame.size.height-280,250, 30);
    }else{
            sationLbl.frame=CGRectMake(backgroundcolorview.frame.size.width-200, backgroundcolorview.frame.size.height-150,250, 30);
    }
    [sationLbl setFont:[UIFont systemFontOfSize:25.0f]];
    [sationLbl setBackgroundColor:[UIColor clearColor]];
    [sationLbl setTextColor:[UIColor whiteColor]];
    [sationLbl setText:@"Station No:1"];
    sationLbl.lineBreakMode = NSLineBreakByWordWrapping;
    sationLbl.numberOfLines = 2;
    [sationLbl setTextAlignment:NSTextAlignmentLeft];
    [backgroundcolorview addSubview:sationLbl];
    
    Timelbl=[[UILabel alloc] init];
    if (isLandScope){
        Timelbl.frame=CGRectMake(backgroundcolorview.frame.size.width-250, backgroundcolorview.frame.size.height-240,250, 30);
    }else{
        Timelbl.frame=CGRectMake(backgroundcolorview.frame.size.width-250, backgroundcolorview.frame.size.height-100,250, 30);
    }
    [Timelbl setFont:[UIFont systemFontOfSize:25.0f]];
    [Timelbl setBackgroundColor:[UIColor clearColor]];
    [Timelbl setTextColor:[UIColor whiteColor]];
    NSDate *aDate = [NSDate date];
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateStyle:NSDateFormatterShortStyle];
    [formatter setTimeStyle:NSDateFormatterNoStyle];
    [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSString *datestr;
    datestr=[formatter stringFromDate:aDate];
    Timelbl.text=datestr;
    formatter=nil;
    // timer is set & will be triggered each second
    //[NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(showTime) userInfo:nil repeats:YES];
    Timelbl.lineBreakMode = NSLineBreakByWordWrapping;
    Timelbl.numberOfLines = 2;
    [Timelbl setTextAlignment:NSTextAlignmentLeft];
    [backgroundcolorview addSubview:Timelbl];
    
    FileBtn=[[UIButton alloc] init];
    FileBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    FileBtn.frame=CGRectMake(10,25,40,40);
    [FileBtn addTarget:self action:@selector(FileBtnClicked) forControlEvents:UIControlEventTouchUpInside];
    [FileBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    // [FileBtn setTitle:@"file" forState:UIControlStateNormal];
    [FileBtn setBackgroundImage:[UIImage imageNamed:@"optionsicon.png"] forState:UIControlStateNormal];
    [self.view addSubview:FileBtn];
        [paddingview2  removeFromSuperview];
        paddingview2=nil;
        [paddingview  removeFromSuperview];
        paddingview=nil;
    }
    
    y=0;
    if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"7.0"))
    {
        y=y+20;
        
        
    }
    isLandScope = UIInterfaceOrientationIsLandscape([UIApplication sharedApplication].statusBarOrientation);
    
    [self updateUI];
    
    
    [username_text setReturnKeyType:UIReturnKeyNext];
    [password_text setReturnKeyType:UIReturnKeyGo];
    
   
    //[self releaseViews];
}
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:YES];
    username_text.text=@"";password_text.text=@"";
    [login_button setBackgroundImage:[UIImage imageNamed:@"login-button-normal.png"] forState:UIControlStateNormal];
  
      isLandScope = UIInterfaceOrientationIsLandscape([UIApplication sharedApplication].statusBarOrientation);
    [self updateUI];
    
}
-(void)showTime
{

    NSDate *aDate = [NSDate date];
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateStyle:NSDateFormatterShortStyle];
    [formatter setTimeStyle:NSDateFormatterNoStyle];
    [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSString *datestr;
    datestr=[formatter stringFromDate:aDate];
    Timelbl.text=datestr;
     //Timelbl.text=[[NSDate date] description];
    
    
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    if (textField == username_text) {
		[textField resignFirstResponder];
		[password_text becomeFirstResponder];
	}
	else if (textField == password_text) {
		[textField resignFirstResponder];
    }
    return YES;
}

-(void)FileBtnClicked
{
    //////NSLog(@"calld");
    
    if (!FilebackView)
    FilebackView=[[UIView alloc]init];
    if (isLandScope)
    {
          FilebackView.frame=CGRectMake(10,20, 1004, 150);
    }
    else
    {
    FilebackView.frame=CGRectMake(10,20, 748, 150);
    }
    [FilebackView setBackgroundColor:[UIColor colorWithRed:10.0f/255.0f green:10.0f/255.0f blue:10.0f/255.0f alpha:0.7f/1.0f]];
    [FilebackView.layer setBorderWidth:2.0];
    [FilebackView.layer setBorderColor:[[UIColor whiteColor] CGColor]];
    //AddvendorsView.layer.cornerRadius=8;
    [self.view addSubview:FilebackView];
    
    CGFloat Btnw=FilebackView.frame.size.width/4;
    
    if (!RegisterBtn)
    RegisterBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    RegisterBtn.frame=CGRectMake(60,25,Btnw,100);
    [RegisterBtn addTarget:self action:@selector(RegisterBtnClicked) forControlEvents:UIControlEventTouchUpInside];
    [RegisterBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    //[RegisterBtn setTitle:@"Register" forState:UIControlStateNormal];
    [RegisterBtn setBackgroundImage:[UIImage imageNamed:@"Registerbutton.jpg"] forState:UIControlStateNormal];
    [FilebackView addSubview:RegisterBtn];
   
    
    if (!AboutBtn)
    AboutBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    AboutBtn.frame=CGRectMake(60+RegisterBtn.frame.size.width+25,25,Btnw,100);
    [AboutBtn addTarget:self action:@selector(AboutBtnClicked) forControlEvents:UIControlEventTouchUpInside];
    [AboutBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    //[AboutBtn setTitle:@"About" forState:UIControlStateNormal];
    [AboutBtn setBackgroundImage:[UIImage imageNamed:@"about_button.jpg"] forState:UIControlStateNormal];
    [FilebackView addSubview:AboutBtn];
   
    
    if (!ExitFileBtn)
    ExitFileBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    ExitFileBtn.frame=CGRectMake(60+RegisterBtn.frame.size.width+20+AboutBtn.frame.size.width+30,25,Btnw,100);
    [ExitFileBtn addTarget:self action:@selector(ExitBtnClicked) forControlEvents:UIControlEventTouchUpInside];
     [ExitFileBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
   // [ExitFileBtn setTitle:@"Exit" forState:UIControlStateNormal];
    [ExitFileBtn setBackgroundImage:[UIImage imageNamed:@"exit_button.jpg"] forState:UIControlStateNormal];
    [FilebackView addSubview:ExitFileBtn];
   
    
    
}
-(void)ExitBtnClicked
{
    NSArray *mainArray = FilebackView.subviews;
    for (int i = 0; i<[mainArray count]; i++) {
        UIView *vi =[mainArray objectAtIndex:i];
        [vi removeFromSuperview];
        vi = nil;
    }
    [FilebackView removeFromSuperview];
    
    
}
-(void)AboutBtnClicked
{
   
    NSArray *mainArray = FilebackView.subviews;
    for (int i = 0; i<[mainArray count]; i++) {
        UIView *vi =[mainArray objectAtIndex:i];
        [vi removeFromSuperview];
        vi = nil;
    }
    [FilebackView removeFromSuperview];
    if (!Aboutpopup)
    Aboutpopup=[[UIView alloc]init];
    if (isLandScope) {
        Aboutpopup.frame=CGRectMake(0, 0, 1024, 768);
    }
    
    else{
        
        Aboutpopup.frame=CGRectMake(0, 0, 768, 1024);
    }
    [Aboutpopup setBackgroundColor:[UIColor colorWithRed:10.0f/255.0f green:10.0f/255.0f blue:10.0f/255.0f alpha:0.7f/1.0f]];
    [self.view addSubview:Aboutpopup];
    
    if (!AboutbackView)
    AboutbackView=[[UIScrollView alloc]init];
    if (isLandScope) {
        AboutbackView.frame=CGRectMake(250, 20, 500, 748-40);
        
        AboutbackView.contentSize=CGSizeMake(500, 1000);
    }else{
        
        AboutbackView.frame=CGRectMake(130,50, 500, 1004-200);
    }
    [AboutbackView setBackgroundColor:[UIColor whiteColor]];
    [AboutbackView.layer setBorderWidth:2.0];
    AboutbackView.userInteractionEnabled=YES;
    [AboutbackView.layer setBorderColor:[[UIColor blackColor] CGColor]];
    // [filepopupView setBackgroundColor:[UIColor lightGrayColor]];
    [Aboutpopup addSubview:AboutbackView];
  
    if (!logoImageView)
    logoImageView=[[UIImageView alloc]init];
    logoImageView.frame=CGRectMake(10, 10,100 , 100);
    [logoImageView setImage:[UIImage imageNamed:@"aone_logo_action.png"]];
    [AboutbackView addSubview:logoImageView];
    if (!Titlelbl)
    Titlelbl=[[UILabel alloc] init];
    Titlelbl.frame=CGRectMake(logoImageView.frame.size.width+80,40,AboutbackView.frame.size.width-logoImageView.frame.size.width, 30);
    [Titlelbl setFont:[UIFont systemFontOfSize:40.0f]];
    [Titlelbl setBackgroundColor:[UIColor clearColor]];
    [Titlelbl setTextColor:[UIColor blackColor]];
    [Titlelbl setText:@"A ONE POS"];
    Titlelbl.lineBreakMode = NSLineBreakByWordWrapping;
    Titlelbl.numberOfLines = 2;
    [Titlelbl setTextAlignment:NSTextAlignmentLeft];
    [AboutbackView addSubview:Titlelbl];
    if (!subtitlelbl)
    subtitlelbl=[[UILabel alloc] init];
    subtitlelbl.frame=CGRectMake(logoImageView.frame.size.width+60,80,AboutbackView.frame.size.width-logoImageView.frame.size.width, 30);
    [subtitlelbl setFont:[UIFont systemFontOfSize:15.0f]];
    [subtitlelbl setBackgroundColor:[UIColor clearColor]];
    [subtitlelbl setTextColor:[UIColor blackColor]];
    [subtitlelbl setText:@"Retails  and  Restaurant  Versions"];
    subtitlelbl.lineBreakMode = NSLineBreakByWordWrapping;
    subtitlelbl.numberOfLines = 2;
    [subtitlelbl setTextAlignment:NSTextAlignmentLeft];
    [AboutbackView addSubview:subtitlelbl];
    
    NSMutableString *stringToPrint;
    if (!stringToPrint)
   stringToPrint = [[NSMutableString alloc] initWithString:@""];
    
    [stringToPrint appendFormat:@"    Retail Cash Register \n \n   AonePos Software and Database Version  7.0  \n  Production Version: 7.0  \n \n Designed and Developed By \n  \t\tAonePos \n \n   RCR 2012 20012-2013 AonePos \n \n   Model: Retail Cash Register Copyright \n 2012-2014 AonePos \n \n \n\n\n  "];
    
    if (!Abouttextlbl)
    Abouttextlbl=[[UITextView alloc] init];
    Abouttextlbl.frame=CGRectMake(20,150,AboutbackView.frame.size.width-40,400);
    [Abouttextlbl setFont:[UIFont systemFontOfSize:20.0f]];
    [Abouttextlbl setBackgroundColor:[UIColor clearColor]];
    [Abouttextlbl setTextColor:[UIColor blackColor]];
    [Abouttextlbl setText:stringToPrint];
    //Abouttextlbl.lineBreakMode = NSLineBreakByWordWrapping;
    //Abouttextlbl.numberOfLines = 2;
    [Abouttextlbl setTextAlignment:NSTextAlignmentLeft];
    [AboutbackView addSubview:Abouttextlbl];
    
    if (!ViewLiceseBtn)
    ViewLiceseBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    ViewLiceseBtn.frame=CGRectMake(20,450,AboutbackView.frame.size.width-40,40);
    [ViewLiceseBtn addTarget:self action:@selector(ViewlicenceBtnClicked) forControlEvents:UIControlEventTouchUpInside];
    [ViewLiceseBtn setUserInteractionEnabled:YES];
    [ViewLiceseBtn setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    [ViewLiceseBtn setTitle:@"View Licence Agreement" forState:UIControlStateNormal];
    ViewLiceseBtn.titleLabel.font=[UIFont systemFontOfSize:25.0f];
    //[OkAboutBtn setBackgroundImage:[UIImage imageNamed:@"okay-button.png"] forState:UIControlStateNormal];
    [AboutbackView addSubview:ViewLiceseBtn];
    
    NSMutableString *stringToPrint2;
    if (!stringToPrint2)
    stringToPrint2 = [[NSMutableString alloc] initWithString:@""];
    [stringToPrint2 appendFormat:@"NTEP Ceritificate of Conformance# \n \n  View Cerificate:PDF \n \n Operating System: IOS 7.0\n    Ipad Name: Store ID: 01"];
    if (!ViewLicencetextview)
    ViewLicencetextview=[[UITextView alloc] init];
    ViewLicencetextview.frame=CGRectMake(20,500,AboutbackView.frame.size.width-40,200);
    [ViewLicencetextview setFont:[UIFont systemFontOfSize:20.0f]];
    [ViewLicencetextview setBackgroundColor:[UIColor clearColor]];
    [ViewLicencetextview setTextColor:[UIColor blackColor]];
    [ViewLicencetextview setText:stringToPrint2];
    [ViewLicencetextview setTextAlignment:NSTextAlignmentLeft];
    [AboutbackView addSubview:ViewLicencetextview];

    if (!OkAboutBtn)
    OkAboutBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    OkAboutBtn.frame=CGRectMake(200,720,100,40);
    [OkAboutBtn addTarget:self action:@selector(okBtnClicked) forControlEvents:UIControlEventTouchUpInside];
    [OkAboutBtn setUserInteractionEnabled:YES];
    [OkAboutBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [OkAboutBtn setTitle:@"ok" forState:UIControlStateNormal];
    [OkAboutBtn setBackgroundImage:[UIImage imageNamed:@"additionaloptionspressed.png"] forState:UIControlStateNormal];
    [AboutbackView addSubview:OkAboutBtn];
    [AboutbackView setUserInteractionEnabled:YES];
    
}

-(void)ViewlicenceBtnClicked
{
   // ////NSLog(@"view button clicked");
    if (!Licencepopup)
    Licencepopup=[[UIView alloc]init];
    if (isLandScope) {
        Licencepopup.frame=CGRectMake(0, 0, 1024, 768);
    }
    
    else{
        
        Licencepopup.frame=CGRectMake(0, 0, 768, 1024);
    }
    [Licencepopup setBackgroundColor:[UIColor colorWithRed:10.0f/255.0f green:10.0f/255.0f blue:10.0f/255.0f alpha:0.7f/1.0f]];
    [self.view addSubview:Licencepopup];
    
    if (!LicenceView)
    LicenceView=[[UIScrollView alloc]init];
    LicenceView.frame=CGRectMake(20, 40, Licencepopup.frame.size.width-40, Licencepopup.frame.size.height-80);
    [LicenceView setBackgroundColor:[UIColor colorWithRed:0.0f green:74.0f/255.0f blue:132.0f/255.0f alpha:1.0f]];
    LicenceView.contentSize=CGSizeMake(Licencepopup.frame.size.width-40, Licencepopup.frame.size.height-20+300);
    [LicenceView.layer setBorderWidth:2.0];
    [LicenceView.layer setBorderColor:[[UIColor whiteColor] CGColor]];
    //AddvendorsView.layer.cornerRadius=8;
    [Licencepopup addSubview:LicenceView];
    if(!LicenceWebView)
    {
        @autoreleasepool {
    LicenceWebView=[[UIWebView alloc]initWithFrame:(CGRectMake(20, 10,LicenceView.frame.size.width-40, LicenceView.frame.size.height-100))];
    NSString *htmlFile = [[NSBundle mainBundle] pathForResource:@"register" ofType:@"html"];
    NSString* htmlString = [NSString stringWithContentsOfFile:htmlFile encoding:NSUTF8StringEncoding error:nil];
    [LicenceWebView loadHTMLString:htmlString baseURL:nil];
    //NSURLRequest *urlRequest=[NSURLRequest requestWithURL:[generalDataObject htmlurl]];
    
    //[LicenceWebView loadRequest:urlRequest];
    LicenceWebView.delegate=self;
    [LicenceWebView setBackgroundColor:[UIColor whiteColor]];
    [LicenceView addSubview:LicenceWebView];
        }
    }
    CGFloat value=LicenceView.frame.size.width/3;
    if (!AceeptBtn)
    AceeptBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    AceeptBtn.frame=CGRectMake(value+30,LicenceView.frame.size.height-65,100,40);
    [AceeptBtn addTarget:self action:@selector(acceptLicenceClicked) forControlEvents:UIControlEventTouchUpInside];
    [AceeptBtn setUserInteractionEnabled:YES];
    [AceeptBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [AceeptBtn setTitle:@"Accept" forState:UIControlStateNormal];
    [AceeptBtn setBackgroundImage:[UIImage imageNamed:@"additionaloptionspressed.png"] forState:UIControlStateNormal];
    [LicenceView addSubview:AceeptBtn];
    
    if (!CancelBtn)
    CancelBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    CancelBtn.frame=CGRectMake(value+180,LicenceView.frame.size.height-65,100,40);
    [CancelBtn addTarget:self action:@selector(cancellicenceClicked) forControlEvents:UIControlEventTouchUpInside];
    [CancelBtn setUserInteractionEnabled:YES];
    [CancelBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [CancelBtn setTitle:@"Cancel" forState:UIControlStateNormal];
    [CancelBtn setBackgroundImage:[UIImage imageNamed:@"additionaloptionspressed.png"] forState:UIControlStateNormal];
    [LicenceView addSubview:CancelBtn];
    
}
-(void)acceptLicenceClicked
{
    NSArray *LicenceViewArray = LicenceView.subviews;
    for (int i = 0; i<[LicenceViewArray count]; i++) {
        UIView *vi =[LicenceViewArray objectAtIndex:i];
        [vi removeFromSuperview];
        vi = nil;
    }
    NSArray *LicencepopupArray = Licencepopup.subviews;
    for (int i = 0; i<[LicencepopupArray count]; i++) {
        UIView *vi =[LicencepopupArray objectAtIndex:i];
        [vi removeFromSuperview];
        vi = nil;
    }
    [LicenceWebView loadRequest:nil];
    [Licencepopup removeFromSuperview],[LicenceView removeFromSuperview], [LicenceWebView removeFromSuperview];

}
-(void)cancellicenceClicked
{
    NSArray *LicenceViewArray = LicenceView.subviews;
    for (int i = 0; i<[LicenceViewArray count]; i++) {
        UIView *vi =[LicenceViewArray objectAtIndex:i];
        [vi removeFromSuperview];
        vi = nil;
    }
    NSArray *LicencepopupArray = Licencepopup.subviews;
    for (int i = 0; i<[LicencepopupArray count]; i++) {
        UIView *vi =[LicencepopupArray objectAtIndex:i];
        [vi removeFromSuperview];
        vi = nil;
    }
    [LicenceWebView loadRequest:nil];
    [Licencepopup removeFromSuperview],[LicenceView removeFromSuperview],[LicenceWebView removeFromSuperview];
}

-(void)okBtnClicked
{
    NSArray *LicenceViewArray = AboutbackView.subviews;
    for (int i = 0; i<[LicenceViewArray count]; i++) {
        UIView *vi =[LicenceViewArray objectAtIndex:i];
        [vi removeFromSuperview];
        vi = nil;
    }
    NSArray *LicencepopupArray = Aboutpopup.subviews;
    for (int i = 0; i<[LicencepopupArray count]; i++) {
        UIView *vi =[LicencepopupArray objectAtIndex:i];
        [vi removeFromSuperview];
        vi = nil;
    }
   
     [AboutbackView removeFromSuperview],[Aboutpopup removeFromSuperview];
   
    
}
-(void)RegisterBtnClicked
{
    NSArray *mainArray = FilebackView.subviews;
    for (int i = 0; i<[mainArray count]; i++) {
        UIView *vi =[mainArray objectAtIndex:i];
        [vi removeFromSuperview];
        vi = nil;
    }
    [FilebackView removeFromSuperview];
  
    if (!RegisterpopUp)
        RegisterpopUp=[[UIView alloc]init];
        if (isLandScope){
            RegisterpopUp.frame=CGRectMake(0, 0, 1024, 768);
        }else{
            
            RegisterpopUp.frame=CGRectMake(0, 0, 768, 1024);
        }
        [RegisterpopUp setBackgroundColor:[UIColor colorWithRed:10.0f/255.0f green:10.0f/255.0f blue:10.0f/255.0f alpha:0.7f/1.0f]];
        [self.view addSubview:RegisterpopUp];
    
  
    if (!RegisterView)
    RegisterView=[[UIScrollView alloc]init];
    if (isLandScope) {
        RegisterView.frame=CGRectMake(250, 20, 500, 748-40);
        
        RegisterView.contentSize=CGSizeMake(500, 1000);
    }else{
        
        RegisterView.frame=CGRectMake(130,50, 500, 1004-300);
    }
    [RegisterView setBackgroundColor:[UIColor whiteColor]];
    [RegisterView.layer setBorderWidth:2.0];
    RegisterView.userInteractionEnabled=YES;
    [RegisterView.layer setBorderColor:[[UIColor blackColor] CGColor]];
    // [filepopupView setBackgroundColor:[UIColor lightGrayColor]];
    [RegisterpopUp addSubview:RegisterView];
    //tapRecognizer.cancelsTouchesInView = NO;
    
    if (!ReglogoImageView)
    ReglogoImageView=[[UIImageView alloc]init];
    ReglogoImageView.frame=CGRectMake(10, 10,100 , 100);
    [ReglogoImageView setImage:[UIImage imageNamed:@"aone_logo_action.png"]];
    [RegisterView addSubview:ReglogoImageView];
    
    if (!RegTitlelbl)
    RegTitlelbl=[[UILabel alloc] init];
    RegTitlelbl.frame=CGRectMake(ReglogoImageView.frame.size.width+80,40,RegisterView.frame.size.width-ReglogoImageView.frame.size.width, 30);
    [RegTitlelbl setFont:[UIFont systemFontOfSize:40.0f]];
    [RegTitlelbl setBackgroundColor:[UIColor clearColor]];
    [RegTitlelbl setTextColor:[UIColor blackColor]];
    [RegTitlelbl setText:@"A ONE POS"];
    RegTitlelbl.lineBreakMode = NSLineBreakByWordWrapping;
    RegTitlelbl.numberOfLines = 2;
    [RegTitlelbl setTextAlignment:NSTextAlignmentLeft];
    [RegisterView addSubview:RegTitlelbl];
    
    if (!Regsubtitlelbl)
    Regsubtitlelbl=[[UILabel alloc] init];
    Regsubtitlelbl.frame=CGRectMake(ReglogoImageView.frame.size.width+60,80,RegisterView.frame.size.width-ReglogoImageView.frame.size.width, 30);
    [Regsubtitlelbl setFont:[UIFont systemFontOfSize:18.0f]];
    [Regsubtitlelbl setBackgroundColor:[UIColor clearColor]];
    [Regsubtitlelbl setTextColor:[UIColor blackColor]];
    [Regsubtitlelbl setText:@"Retails  and  Restaurant  Versions"];
    Regsubtitlelbl.lineBreakMode = NSLineBreakByWordWrapping;
    Regsubtitlelbl.numberOfLines = 2;
    [Regsubtitlelbl setTextAlignment:NSTextAlignmentLeft];
    [RegisterView addSubview:Regsubtitlelbl];
    
    CGFloat ReWidth=RegisterView.frame.size.width/2;
    if (!Namelbl)
    Namelbl=[[UILabel alloc] init];
    Namelbl.frame=CGRectMake(10, 150,ReWidth-40, 40);
    [Namelbl setFont:[UIFont systemFontOfSize:25.0f]];
    [Namelbl setBackgroundColor:[UIColor clearColor]];
    [Namelbl setTextColor:[UIColor blackColor]];
    [Namelbl setText:@"Name"];
    [Namelbl setTextAlignment:NSTextAlignmentLeft];
    [RegisterView addSubview:Namelbl];
    
    if (!NameField)
    NameField=[[UITextField alloc] init];
    //[nameTextfield setBorderStyle:UITextBorderStyleRoundedRect];
    NameField.frame=CGRectMake(Namelbl.frame.size.width, 150,ReWidth+30, 40);
    NameField.delegate=self;
    UIView *paddingview=[[UIView alloc] initWithFrame:CGRectMake(5, 0, 5, 20)];
    // ////NSLog(@"%f",GeneralinfoField.frame.origin.y);
    NameField.leftView = paddingview;
    NameField.contentVerticalAlignment=UIControlContentVerticalAlignmentCenter;
    NameField.leftViewMode = UITextFieldViewModeAlways;
    [NameField setBackground:[UIImage imageNamed:@"addinputtextfield.png"]];
    [NameField setAutocorrectionType:UITextAutocorrectionTypeNo];
    [RegisterView addSubview:NameField];
    [paddingview removeFromSuperview];
    paddingview=nil;
    if (!Emaillbl)
    Emaillbl=[[UILabel alloc] init];
    Emaillbl.frame=CGRectMake(10, 240,ReWidth-40, 40);
    [Emaillbl setFont:[UIFont systemFontOfSize:25.0f]];
    [Emaillbl setBackgroundColor:[UIColor clearColor]];
    [Emaillbl setTextColor:[UIColor blackColor]];
    [Emaillbl setText:@"Email"];
    [Emaillbl setTextAlignment:NSTextAlignmentLeft];
    [RegisterView addSubview:Emaillbl];
    
    if (!EmailField)
    EmailField=[[UITextField alloc] init];
    //[nameTextfield setBorderStyle:UITextBorderStyleRoundedRect];
    EmailField.frame=CGRectMake(Namelbl.frame.size.width, 240,ReWidth+30, 40);
    EmailField.delegate=self;
    UIView *paddingview2=[[UIView alloc] initWithFrame:CGRectMake(5, 0, 5, 20)];
    // ////NSLog(@"%f",GeneralinfoField.frame.origin.y);
    EmailField.leftView = paddingview2;
    [paddingview2 removeFromSuperview];
    paddingview2=nil;
    EmailField.contentVerticalAlignment=UIControlContentVerticalAlignmentCenter;
    EmailField.leftViewMode = UITextFieldViewModeAlways;
    [EmailField setBackground:[UIImage imageNamed:@"addinputtextfield.png"]];
    [EmailField setAutocorrectionType:UITextAutocorrectionTypeNo];
     EmailField.autocapitalizationType = UITextAutocapitalizationTypeNone;
    [RegisterView addSubview:EmailField];
    
    if (!PhNumberlbl)
    PhNumberlbl=[[UILabel alloc] init];
    PhNumberlbl.frame=CGRectMake(10, 330,ReWidth-40, 40);
    [PhNumberlbl setFont:[UIFont systemFontOfSize:25.0f]];
    [PhNumberlbl setBackgroundColor:[UIColor clearColor]];
    [PhNumberlbl setTextColor:[UIColor blackColor]];
    [PhNumberlbl setText:@"PhoneNumber"];
    [PhNumberlbl setTextAlignment:NSTextAlignmentLeft];
    [RegisterView addSubview:PhNumberlbl];
    
    if (!PhNumberField)
    PhNumberField=[[UITextField alloc] init];
    //[nameTextfield setBorderStyle:UITextBorderStyleRoundedRect];
    PhNumberField.frame=CGRectMake(Namelbl.frame.size.width, 330,ReWidth+30, 40);
    PhNumberField.delegate=self;
    UIView *paddingview3=[[UIView alloc] initWithFrame:CGRectMake(5, 0, 5, 20)];
    // ////NSLog(@"%f",GeneralinfoField.frame.origin.y);
    PhNumberField.leftView = paddingview3;
    [paddingview3 removeFromSuperview];
    paddingview3=nil;
    PhNumberField.contentVerticalAlignment=UIControlContentVerticalAlignmentCenter;
    PhNumberField.leftViewMode = UITextFieldViewModeAlways;
    PhNumberField.keyboardType=UIKeyboardTypeNumberPad;
    [PhNumberField setBackground:[UIImage imageNamed:@"addinputtextfield.png"]];
    [PhNumberField setAutocorrectionType:UITextAutocorrectionTypeNo];
    [RegisterView addSubview:PhNumberField];
    
    if (!Companynamelbl)
    Companynamelbl=[[UILabel alloc] init];
    Companynamelbl.frame=CGRectMake(10, 420,ReWidth-40, 40);
    [Companynamelbl setFont:[UIFont systemFontOfSize:25.0f]];
    [Companynamelbl setBackgroundColor:[UIColor clearColor]];
    [Companynamelbl setTextColor:[UIColor blackColor]];
    [Companynamelbl setText:@"CompanyName"];
    [Companynamelbl setTextAlignment:NSTextAlignmentLeft];
    [RegisterView addSubview:Companynamelbl];
    
    if (!CompanynameField)
    CompanynameField=[[UITextField alloc] init];
    //[nameTextfield setBorderStyle:UITextBorderStyleRoundedRect];
    CompanynameField.frame=CGRectMake(Namelbl.frame.size.width, 420,ReWidth+30, 40);
    CompanynameField.delegate=self;
    UIView *paddingview4=[[UIView alloc] initWithFrame:CGRectMake(5, 0, 5, 20)];
    // ////NSLog(@"%f",GeneralinfoField.frame.origin.y);
    CompanynameField.leftView = paddingview4;
    [paddingview4 removeFromSuperview];
    paddingview4=nil;
    CompanynameField.contentVerticalAlignment=UIControlContentVerticalAlignmentCenter;
    CompanynameField.leftViewMode = UITextFieldViewModeAlways;
    [CompanynameField setBackground:[UIImage imageNamed:@"addinputtextfield.png"]];
    [CompanynameField setAutocorrectionType:UITextAutocorrectionTypeNo];
     CompanynameField.autocapitalizationType = UITextAutocapitalizationTypeNone;
    [RegisterView addSubview:CompanynameField];
    
    if (!RegisterNowBtn)
    RegisterNowBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    RegisterNowBtn.frame=CGRectMake(30,550,ReWidth-30,50);
    [RegisterNowBtn addTarget:self action:@selector(RegNowClicked) forControlEvents:UIControlEventTouchUpInside];
    [RegisterNowBtn setUserInteractionEnabled:YES];
    [RegisterNowBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [RegisterNowBtn setTitle:@"Register Now" forState:UIControlStateNormal];
    [RegisterNowBtn setBackgroundImage:[UIImage imageNamed:@"additionaloptionspressed.png"] forState:UIControlStateNormal];
    [RegisterView addSubview:RegisterNowBtn];
    
    if (!ReglaterBtn)
    ReglaterBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    ReglaterBtn.frame=CGRectMake(ReWidth+10,550,ReWidth-30,50);
    [ReglaterBtn addTarget:self action:@selector(ReglaterClicked) forControlEvents:UIControlEventTouchUpInside];
    [ReglaterBtn setUserInteractionEnabled:YES];
    [ReglaterBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [ReglaterBtn setTitle:@"Register Later" forState:UIControlStateNormal];
    [ReglaterBtn setBackgroundImage:[UIImage imageNamed:@"additionaloptionspressed.png"] forState:UIControlStateNormal];
    [RegisterView addSubview:ReglaterBtn];
    
}
-(void)RegNowClicked
{
     NSMutableDictionary *JsonSendDataDict=Nil;
   
    JsonSendDataDict=[[NSMutableDictionary alloc]init];
   
    NSDictionary *myDict=[[NSDictionary alloc]initWithObjectsAndKeys:NameField.text,@"name",EmailField.text,@"email",PhNumberField.text,@"phone",CompanynameField.text,@"company", nil];
    
    NSMutableArray *jsonArray=[NSMutableArray arrayWithObjects:myDict, nil];
    [JsonSendDataDict setObject:jsonArray forKey:@"fields"];
    
    NSDictionary *jsonDict = [NSDictionary dictionaryWithDictionary:JsonSendDataDict];

    NSError *err;
    
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:jsonDict options:kNilOptions error:&err];
    NSString *str=[[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
   if(![generalDataObject connectedToInternet])
    {
        
    }
    else if([generalDataObject  connectedToInternet]){

       ////NSLog(@"%@",str);
        NSString *string=[NSString stringWithFormat:@"http://192.168.1.6/xampp/sites/aone/crm/customer-registration.php?name=%@&email=%@&phone=%@&company=%@",[NameField.text stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding],[EmailField.text stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding],[PhNumberField.text stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding],[CompanynameField.text stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
       // ////NSLog(@"%@",string);
        
        
        NSURL *url=[NSURL URLWithString:string];
       // ////NSLog(@"%@",url);
        
        NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
        RegisterConnection = [NSURLConnection connectionWithRequest:request delegate:self];
        
        [RegisterConnection start];
        string=nil;
    }
    NSArray *RegisterpopUpArray = RegisterpopUp.subviews;
    for (int i = 0; i<[RegisterpopUpArray count]; i++) {
        UIView *vi =[RegisterpopUpArray objectAtIndex:i];
        [vi removeFromSuperview];
        vi = nil;
    }
    [RegisterpopUp removeFromSuperview];
   
    myDict=nil;
    [jsonArray removeAllObjects];
    jsonArray=nil;
    jsonData=nil;
    str=nil;
}
-(void)ReglaterClicked
{
    NSArray *RegisterpopUpArray = RegisterpopUp.subviews;
    for (int i = 0; i<[RegisterpopUpArray count]; i++) {
        UIView *vi =[RegisterpopUpArray objectAtIndex:i];
        [vi removeFromSuperview];
        vi = nil;
    }
    [RegisterpopUp removeFromSuperview];
    
}
-(void)updateTimer
{
    NSDateFormatter *dateformatter=[[NSDateFormatter alloc]init];
    
    [dateformatter setDateFormat:@"MM-dd-yyyy HH:mm:ss"];
    
    NSString *dateInStringFormated=[dateformatter stringFromDate:[NSDate date]];
     [Timelbl setText:dateInStringFormated];
    dateInStringFormated=nil;
    dateformatter=nil;
    
}

-(void)LoginDatabasesaveMethod
{
    [generalDataObject GetuserDataVariablesMethod];
    
    NSString * serverlocol=@"local";
    
    //logintime Details
   
     AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
    appDelegate.loginSessionID=[generalDataObject deviceID];
    NSString * insertSQL =@"insert into login_logout_table(login_employee_name, login_employee_id,login_time,logout_time,diff_minutes,diff_hours,wages,sessioniduniqueval,unique_id,created_timestamp,modified_timestamp,server_local) VALUES (?,?,?,?,?,?,?,?,?,?,?,?)";
    
    NSMutableArray *querysArray=[[NSMutableArray alloc] initWithObjects:username_text.text, username_text.text,[generalDataObject Datestring],@"",@"",@"",@"",appDelegate.loginSessionID,[generalDataObject genRandStringLength:20],[generalDataObject Datestring],[generalDataObject Datestring],serverlocol, nil];
    
   // //NSLog(@"%@ %@",insertSQL,querysArray);
    [[DataBaseManager dataBaseManager] executeRecordsWithQuery:insertSQL withArguments:querysArray];
    //[dbObject insertMethod:insertSQL];
    insertSQL=nil;
    
    
    NSString * selectQuery =[NSString stringWithFormat:@"select login_employee_name, login_employee_id,login_time,logout_time,diff_minutes,diff_hours,wages,sessioniduniqueval,unique_id,created_timestamp,modified_timestamp,server_local from login_logout_table"];
    NSMutableArray *employeeArray=[[NSMutableArray alloc]init];
    [[DataBaseManager dataBaseManager] execute:selectQuery resultsArray:employeeArray];
   // ////NSLog(@"%@ employee login Array ",employeeArray);
    NSMutableDictionary *CategoryDict=[[NSMutableDictionary alloc]init];
    [CategoryDict setObject:employeeArray forKey:@"fields"];
    NSDictionary *jsonDict = [NSDictionary dictionaryWithDictionary:CategoryDict];
    
    NSError *err;
    
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:jsonDict options:kNilOptions error:&err];
    
    NSString *str=[[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
    
    //////NSLog(@"%@", str);
    
   if(![generalDataObject connectedToInternet]){
        
        NSString *insertSQL =@"insert into pending_queries (query_type,user_id,page_url,parameters,table_name,current_time) values(?,?,?,?,?,?)";
       NSMutableArray *pendingArray=[[NSMutableArray alloc]initWithObjects:@"INSERT",[generalDataObject userID],@"saveinfo.php?",str,@"login_logout_table",[generalDataObject Datestring], nil];
       
       [[DataBaseManager dataBaseManager] executeRecordsWithQuery:insertSQL withArguments:pendingArray];
        
    }else if([generalDataObject connectedToInternet]){
        
        if ([[generalDataObject OriginalbaseURLstr] length]>0){
            
        
            NSString *string=[NSString stringWithFormat:@"%@saveinfo.php?tablename=login_logout_table&systemtime=%@&userid=%@&sessionid=%@&data=%@",[generalDataObject OriginalbaseURLstr],[[generalDataObject Datestring] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding],[generalDataObject userID],[generalDataObject genRandStringLength:20],[str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
            
            //////NSLog(@"%@",str);
            
            NSURL *url=[NSURL URLWithString:string];
            NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url];
            //////NSLog(@"%@",url);
            
            [request setHTTPMethod:@"POST"];
            [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
            //[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
            
            loginConnection = [NSURLConnection connectionWithRequest:request delegate:self];
            
            [loginConnection start];
            string=nil;
            request=nil;
            url=nil;
            
        }
    }
    jsonData=nil;
    str=nil,jsonDict=nil,CategoryDict=nil,serverlocol=nil;
}

-(void)loginClicked
{
    
    NSString *usernamestr=@"01";
    NSString *passwordstr=@"admin";
    [login_button setBackgroundImage:[UIImage imageNamed:@"login-button-pressed.png"] forState:UIControlStateNormal];
    username=[[NSString alloc]init];
    username=username_text.text;
    password=[[NSString alloc]init];
    password=[password encodeString:password_text.text] ;
    //password=password_text.text;
    if ((username_text.text.length!=0) || (password_text.text.length!=0)){
        if ([[generalDataObject  OriginalbaseURLstr] length]==0){
            NSString *useridDb,*passwordDB;
            
            NSString *selectQuery = [NSString stringWithFormat:@"SELECT userid,password FROM admin_details"];
            NSMutableArray *adminArray=[[NSMutableArray alloc]init];
            [[DataBaseManager dataBaseManager]execute:selectQuery resultsArray:adminArray];
            
            if ([adminArray count]>0){
                NSDictionary *myDict=[adminArray  objectAtIndex:0];
                useridDb=[myDict objectForKey:@"userid"];
                passwordDB=[myDict objectForKey:@"password"];
            }
            
            if ([username isEqualToString:useridDb] && [password isEqualToString:passwordDB]){
                
                
                MainView *mainViewObj=[[MainView alloc]initWithNibName:@"MainView" bundle:nil];
                [self.navigationController pushViewController:mainViewObj animated:NO];
              [self SaveMislaneousDataMethd:useridDb usertype:@"admin" sessionID:@""];
                
                [self LoginDatabasesaveMethod];
                [self releaseViews];
            }else{
                
                NSString *querySQL = [NSString stringWithFormat: @"SELECT employee_department,employee_employee_id FROM employee_table WHERE employee_employee_id='%@' AND employee_password='%@'",username,password];
                NSMutableArray *employeeArray=[[NSMutableArray alloc]init];
                [[DataBaseManager dataBaseManager]execute:querySQL resultsArray:employeeArray];
                if ([employeeArray count]>0){
                    
                    MainView *mainViewObj=[[MainView alloc]initWithNibName:@"MainView" bundle:nil];
                    [self.navigationController pushViewController:mainViewObj animated:NO];
                    //[self.navigationController pushViewController:mainview animated:NO];
                    
                  [self SaveMislaneousDataMethd:username usertype:@"employee" sessionID:@""];
                    
                    [self LoginDatabasesaveMethod];
                    [self releaseViews];
                   
                }else{
                    if ([username_text.text isEqualToString:usernamestr] && [password_text.text isEqualToString:passwordstr]){
                        
                        MainView *mainViewObj=[[MainView alloc]initWithNibName:@"MainView" bundle:nil];
                        [self.navigationController pushViewController:mainViewObj animated:NO];
                        
                      [self SaveMislaneousDataMethd:usernamestr usertype:@"admin" sessionID:@""];
                        
                        [self LoginDatabasesaveMethod];
                        [self releaseViews];
                    }else{
                        UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"Log in Failed" message:@"username password could not match" delegate:self cancelButtonTitle:@"ok" otherButtonTitles:nil, nil];
                        [alert show];
                        [login_button setBackgroundImage:[UIImage imageNamed:@"login-button-normal.png"] forState:UIControlStateNormal];
                    }
                    
                }
                
            }
            
            
        }else{
            if(![generalDataObject  connectedToInternet]){
                
                
                
                NSString *insertSQL =@"insert into pending_queries (query_type,user_id,page_url,parameters,table_name,current_time) values(?,?,?,?,?,?)";
                
                NSMutableArray *pendingArray=[[NSMutableArray alloc]initWithObjects:@"login",[generalDataObject  userID],@"%@user-login.php?",[NSString stringWithFormat:@"username=%@&password=%@&deviceid=%@&systemtime=%@",username,password,[generalDataObject  deviceID],[generalDataObject getsystemDatetime]],@"",[generalDataObject getsystemDatetime], nil];
                [[DataBaseManager dataBaseManager]executeRecordsWithQuery:insertSQL withArguments:pendingArray];
                
                NSString *useridDb,*passwordDB;
                
                NSString *selectQuery = [NSString stringWithFormat:@"SELECT userid,password FROM admin_details"];
                NSMutableArray *adminArray=[[NSMutableArray alloc]init];
                [[DataBaseManager dataBaseManager]execute:selectQuery resultsArray:adminArray];
                
                
                if ([adminArray count]>0){
                    NSDictionary *myDict=[adminArray  objectAtIndex:0];
                    
                    useridDb=[myDict objectForKey:@"userid"];
                    passwordDB=[myDict objectForKey:@"password"];
                }
                if ([username isEqualToString:useridDb] && [password isEqualToString:passwordDB]){
                    
                    MainView *mainViewObj=[[MainView alloc]initWithNibName:@"MainView" bundle:nil];
                    [self.navigationController pushViewController:mainViewObj animated:NO];
                    [self SaveMislaneousDataMethd:useridDb usertype:@"admin" sessionID:@""];
                    [self LoginDatabasesaveMethod];
                     [self releaseViews];
                    
                }else{
                    NSString *querySQL = [NSString stringWithFormat: @"SELECT employee_department,employee_employee_id FROM employee_table WHERE employee_employee_id='%@' AND employee_password='%@'",username,password];
                    NSMutableArray *employeeArray=[[NSMutableArray alloc]init];
                    [[DataBaseManager dataBaseManager]execute:querySQL resultsArray:employeeArray];
                    
                    if ([employeeArray count]>0){
                        MainView *mainViewObj=[[MainView alloc]initWithNibName:@"MainView" bundle:nil];
                        [self.navigationController pushViewController:mainViewObj animated:NO];
                       
                        [self SaveMislaneousDataMethd:username usertype:@"employee" sessionID:@""];
                        [self LoginDatabasesaveMethod];
                         [self releaseViews];
                    }else{
                        if ([adminArray count]>0){
                            UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"Log in Failed" message:@"username password could not match" delegate:self cancelButtonTitle:@"ok" otherButtonTitles:nil, nil];
                            [alert show];
                            [login_button setBackgroundImage:[UIImage imageNamed:@"login-button-normal.png"] forState:UIControlStateNormal];
                        }else{
                            
                            if ([username_text.text isEqualToString:usernamestr] && [password_text.text isEqualToString:passwordstr]){
                                MainView *mainViewObj=[[MainView alloc]initWithNibName:@"MainView" bundle:nil];
                                [self.navigationController pushViewController:mainViewObj animated:NO];
                               [self SaveMislaneousDataMethd:usernamestr usertype:@"admin" sessionID:@""];
                                [self LoginDatabasesaveMethod];
                                
                                 [self releaseViews];
                            }else{
                                UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"Log in Failed" message:@"username password could not match" delegate:self cancelButtonTitle:@"ok" otherButtonTitles:nil, nil];
                                [alert show];
                                [login_button setBackgroundImage:[UIImage imageNamed:@"login-button-normal.png"] forState:UIControlStateNormal];
                            }
                        }
                        
                    }
                }
            }
            else
            {
                NSMutableArray *QuerytypesArray,*TablenamesArray,*FieldsArray;
                
                QuerytypesArray=[[NSMutableArray alloc]init];
                
                TablenamesArray=[[NSMutableArray alloc]init];
                FieldsArray=[[NSMutableArray alloc]init];
                //clear pending querys table
                
                [self LoginDatabasesaveMethod];
                
                NSString *selectsql=[NSString stringWithFormat:@"SELECT  query_type,user_id,page_url,parameters,table_name,current_time  FROM pending_queries  where (query_type='INSERT' OR 'UPDATE' OR 'DELETE')AND user_id='%@' AND current_time<'%@'",[generalDataObject  userID],[generalDataObject getsystemDatetime]];
                NSMutableArray *pendingArray=[[NSMutableArray alloc]init];
                [[DataBaseManager dataBaseManager]execute:selectsql resultsArray:pendingArray];
                
                NSString *String;
                if ([pendingArray count]>0){
                    
                    for (NSDictionary *Dict in pendingArray)
                    {
                        [QuerytypesArray addObject:[Dict objectForKey:@"query_type"]];
                        [TablenamesArray addObject:[Dict objectForKey:@"table_name"]];
                        [FieldsArray addObject:[Dict objectForKey:@"parameters"]];
                        
                    }
                    for (int i=0; i<[QuerytypesArray count]; i++)
                    {
                        NSString *Querytype=[QuerytypesArray objectAtIndex:i];
                        NSString *tablename=[TablenamesArray objectAtIndex:i];
                        NSString *parameters=[FieldsArray objectAtIndex:i];
                        
                        if ([Querytype isEqualToString:@"INSERT"]){
                            String=[NSString stringWithFormat:@"%@saveinfo.php?tablename=%@&systemtime=%@&userid=%@&sessionid=%@&data=%@",[generalDataObject  OriginalbaseURLstr],[tablename stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding],[[generalDataObject getsystemDatetime] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding],[generalDataObject  userID],[generalDataObject  genRandStringLength:20],[parameters stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
                            
                            NSURL *url=[NSURL URLWithString:String];
                            NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url];
                            //////NSLog(@"%@",url);
                            
                            [request setHTTPMethod:@"POST"];
                            [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
                            //[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
                            
                            
                            
                            PendimgConnection = [[NSURLConnection alloc]
                                                 initWithRequest:request
                                                 delegate:self startImmediately:NO];
                            
                            [PendimgConnection scheduleInRunLoop:[NSRunLoop mainRunLoop]
                                                         forMode:NSDefaultRunLoopMode];
                            [PendimgConnection start];
                            
                        }
                        if ([Querytype isEqualToString:@"UPDATE"])
                        {
                            
                            String=[NSString stringWithFormat:@"%@saveinfo.php?tablename=%@&systemtime=%@&userid=%@&sessionid=%@&data=%@&update=true",[generalDataObject  OriginalbaseURLstr],[tablename stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding],[[generalDataObject getsystemDatetime] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding],[generalDataObject  userID],[generalDataObject  genRandStringLength:20],[parameters stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
                            NSURL *url=[NSURL URLWithString:String];
                            NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url];
                            // ////NSLog(@"%@",url);
                            
                            [request setHTTPMethod:@"POST"];
                            [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
                            //[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
                            
                            
                            
                            PendimgConnection = [[NSURLConnection alloc]
                                                 initWithRequest:request
                                                 delegate:self startImmediately:NO];
                            
                            [PendimgConnection scheduleInRunLoop:[NSRunLoop mainRunLoop]
                                                         forMode:NSDefaultRunLoopMode];
                            [PendimgConnection start];
                            
                        }
                        if ([Querytype isEqualToString:@"DELETE"])
                        {
                            String=[NSString stringWithFormat:@"%@deleteinfo.php?tablename=%@&systemtime=%@&userid=%@&sessionid=%@&unique_ids=%@",[generalDataObject  OriginalbaseURLstr],[tablename stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding],[[generalDataObject getsystemDatetime] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding],[generalDataObject  userID],[generalDataObject  genRandStringLength:20],[parameters stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
                            NSURL *url=[NSURL URLWithString:String];
                            NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url];
                            ////NSLog(@"%@",url);
                            
                            [request setHTTPMethod:@"POST"];
                            [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
                            //[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
                            
                            PendimgConnection = [[NSURLConnection alloc]
                                                 initWithRequest:request
                                                 delegate:self startImmediately:NO];
                            
                            [PendimgConnection scheduleInRunLoop:[NSRunLoop mainRunLoop]
                                                         forMode:NSDefaultRunLoopMode];
                            [PendimgConnection start];
                        }
                        
                    }
                }
                
                //log in data
                
                NSString *jsonRequest=[[[[@"{\"username\":\"" stringByAppendingString:username] stringByAppendingString:@"\",\"password\":\""] stringByAppendingString:password] stringByAppendingString:@"\"}"];
                
                NSString *geturlstring=[NSString stringWithFormat:@"%@user-login.php?username=%@&password=%@&deviceid=%@&systemtime=%@",[generalDataObject  OriginalbaseURLstr],username,password,[generalDataObject  deviceID],[[generalDataObject getsystemDatetime] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
               // NSLog(@"%@",geturlstring);
                NSURL *url = [NSURL URLWithString:geturlstring];
               // NSLog(@"%@",url);
                NSMutableURLRequest *request ;
                
                request = [[NSMutableURLRequest alloc] initWithURL:url];
                NSData *requestData = [NSData dataWithBytes:[jsonRequest UTF8String] length:[jsonRequest length]];
                
                [request setHTTPMethod:@"POST"];
                [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
                //[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
                [request setValue:[NSString stringWithFormat:@"%d", [requestData length]] forHTTPHeaderField:@"Content-Length"];
                [request setHTTPBody: requestData];
                
                connection1=[NSURLConnection connectionWithRequest:request  delegate:self];
                [connection1 start];
                
            }
            
        }
        
    }
    else
    {
        UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"alert" message:@"fields are mandatory" delegate:self cancelButtonTitle:@"ok" otherButtonTitles:nil, nil];
        [alert show];
        [login_button setBackgroundImage:[UIImage imageNamed:@"login-button-normal.png"] forState:UIControlStateNormal];
        
    }
   
}
-(void)SaveMislaneousDataMethd:(NSString *)userID usertype:(NSString *)userType sessionID:(NSString *)sessionID
{
    NSString *selectQuery=[NSString stringWithFormat:@"select selected_store, local_time,pageurl,last_server_update_time from miscellaneous"];
    NSMutableArray *mislaneousArray=[[NSMutableArray alloc]init];
    [[DataBaseManager dataBaseManager]execute:selectQuery resultsArray:mislaneousArray];
    
    if ([mislaneousArray count]>0){
        NSString *updateSQL =@"UPDATE miscellaneous SET userType=?,sessionId=?,userID=?";
        NSMutableArray *misUpdatearray=[[NSMutableArray alloc]initWithObjects:userType,sessionID,userID, nil];
        [[DataBaseManager dataBaseManager] executeRecordsWithQuery:updateSQL withArguments:misUpdatearray];
        
    }else
    {
        NSString *insertSQL = @"insert into miscellaneous (selected_store,local_time,pageurl,last_server_update_time,userType,sessionId,userID) values(?,?,?,?,?,?,?)";
        NSMutableArray *misUpdatearray=[[NSMutableArray alloc]initWithObjects:@"Store1",[generalDataObject Datestring],[generalDataObject OriginalbaseURLstr],@"",userType,sessionID,userID, nil];
        [[DataBaseManager dataBaseManager]executeRecordsWithQuery:insertSQL withArguments:misUpdatearray];
    }
    
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    
    if (connection == connection1){
        responseData = [[NSMutableData alloc] init];
    }else if (connection == PendimgConnection){
        PendingResponseData = [[NSMutableData alloc] init];
    }else if (connection == RegisterConnection){
        RegisterResponseData = [[NSMutableData alloc] init];
    }else if (connection == loginConnection){
        loginResponseData = [[NSMutableData alloc] init];
    }
    
    
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    
    if (connection == connection1){
        [responseData appendData:data];
    }else if (connection == PendimgConnection){
        [PendingResponseData appendData:data];
    }else if (connection == RegisterConnection){
        [RegisterResponseData appendData:data];
    }else if (connection == loginConnection){
        [loginResponseData appendData:data];
    }
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
   if (connection == connection1){
        NSString *responseStr;
       responseStr = [[NSString alloc] initWithBytes:[responseData mutableBytes] length:[responseData length] encoding:NSUTF8StringEncoding];
        
        NSError *error;
       
        dataDict=[[NSMutableDictionary alloc] init];
        NSData *jsonData = [responseStr dataUsingEncoding:NSUTF8StringEncoding];
        dataDict =[NSJSONSerialization JSONObjectWithData:jsonData options:0 error:&error];
        //NSLog(@"%@",dataDict);
        NSString *status=[dataDict objectForKey:@"login-status"];
        NSString *userid=[dataDict objectForKey:@"userid"];
        NSString *serveruserExist=[dataDict objectForKey:@"server-user-exists"];
        NSString *usertype=[dataDict objectForKey:@"user-type"];
        NSString *sessionID=[dataDict objectForKey:@"sessionid"];
       

      [self SaveMislaneousDataMethd:userid usertype:usertype sessionID:sessionID];
     
        
        NSString *usernamestr=[[dataDict objectForKey:@"recieved-input"] objectForKey:@"username"];
        int statusresult=[status intValue];
        int serverExist=[serveruserExist intValue];
        if (serverExist==1){
            if (statusresult ==1){
            if ([usernamestr isEqualToString:@"01"])
            {
                
                  [self SaveMislaneousDataMethd:usernamestr usertype:@"admin" sessionID:sessionID];
                
            }
                
                MainView *mainViewObj=[[MainView alloc]initWithNibName:@"MainView" bundle:nil];
                [self.navigationController pushViewController:mainViewObj animated:NO];
                 [self releaseViews];
        }
            
        else
        {
            UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"Log in Failed" message:@"username password could not match" delegate:self cancelButtonTitle:@"ok" otherButtonTitles:nil, nil];
            [alert show];
            [login_button setBackgroundImage:[UIImage imageNamed:@"login-button-normal.png"] forState:UIControlStateNormal];
        }
            
     }
        else
        {
            
            NSString *useridDb,*passwordDB;
              NSString *selectQuery = [NSString stringWithFormat:@"SELECT userid,password FROM admin_details"];
            NSMutableArray *adminArray=[[NSMutableArray alloc]init];
            [[DataBaseManager dataBaseManager]execute:selectQuery resultsArray:adminArray];
            
           
            if ([adminArray count]>0)
            {
                NSDictionary *myDict=[adminArray objectAtIndex:0];
                
                //NSString *server=[myDict objectForKey:@"server_local"];
                
                useridDb=[myDict objectForKey:@"userid"];
                passwordDB=[myDict objectForKey:@"password"];
            }
            
            if ([username isEqualToString:useridDb] && [password isEqualToString:passwordDB])
            {
                
                MainView *mainViewObj=[[MainView alloc]initWithNibName:@"MainView" bundle:nil];
                [self.navigationController pushViewController:mainViewObj animated:NO];
                  [self SaveMislaneousDataMethd:useridDb usertype:@"admin" sessionID:sessionID];
                [self LoginDatabasesaveMethod];
                 [self releaseViews];
                
            }
            else
            {
              NSString *querySQL = [NSString stringWithFormat: @"SELECT employee_department,employee_employee_id FROM employee_table WHERE employee_employee_id='%@' AND employee_password='%@'",username,password];
                NSMutableArray *employeeArray=[[NSMutableArray alloc]init];
                [[DataBaseManager dataBaseManager]execute:querySQL resultsArray:employeeArray];
                
                if ([employeeArray count]>0)
                {
                    MainView *mainViewObj=[[MainView alloc]initWithNibName:@"MainView" bundle:nil];
                    [self.navigationController pushViewController:mainViewObj animated:NO];
                    
                    [self SaveMislaneousDataMethd:username usertype:@"employee" sessionID:sessionID];
                    [self LoginDatabasesaveMethod];
                     [self releaseViews];
    
                }
                else
                {
                    if ([adminArray count]>0){
                        UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"Log in Failed" message:@"username password could not match" delegate:self cancelButtonTitle:@"ok" otherButtonTitles:nil, nil];
                        [alert show];
                        [login_button setBackgroundImage:[UIImage imageNamed:@"login-button-normal.png"] forState:UIControlStateNormal];
                    }else{
                        if ([username_text.text isEqualToString:@"01"] && [password_text.text isEqualToString:@"admin"]){
                            MainView *mainViewObj=[[MainView alloc]initWithNibName:@"MainView" bundle:nil];
                            [self.navigationController pushViewController:mainViewObj animated:NO];
                              [self SaveMislaneousDataMethd:@"01" usertype:@"admin" sessionID:sessionID];
                            [self LoginDatabasesaveMethod];
                             [self releaseViews];
                          
                        }
                        
                        else
                        {
                            UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"Log in Failed" message:@"username password could not match" delegate:self cancelButtonTitle:@"ok" otherButtonTitles:nil, nil];
                            [alert show];
                            [login_button setBackgroundImage:[UIImage imageNamed:@"login-button-normal.png"] forState:UIControlStateNormal];
                        }
                    }
                    
                    
                }

        }
        
        
    }
        responseStr=nil;
        
  }else if (connection == PendimgConnection){
      NSString *responseStr;
      responseStr = [[NSString alloc] initWithBytes:[PendingResponseData mutableBytes] length:[PendingResponseData length] encoding:NSUTF8StringEncoding];
        
        NSError *error;
      
        dataDict=[[NSMutableDictionary alloc] init];
        NSData *jsonData = [responseStr dataUsingEncoding:NSUTF8StringEncoding];
        dataDict =[NSJSONSerialization JSONObjectWithData:jsonData options:0 error:&error];
        //////NSLog(@"Pending query data %@",dataDict);
        
        NSMutableArray *deleteArray=[dataDict objectForKey:@"delete-queries"];
        NSMutableArray *insertsArray=[dataDict objectForKey:@"insert-queries"];
       NSMutableArray *deletesArray;
        for (int i=0; i<[deleteArray count]; i++)
        {
            NSString *foo=[deleteArray objectAtIndex:i];
            NSString* bar = [foo stringByReplacingOccurrencesOfString:@"'" withString:@"\""];
            NSString* deletestr = [bar stringByReplacingOccurrencesOfString:@"\\\"" withString:@"'"];
            [[DataBaseManager dataBaseManager]executeRecordsWithQuery:deletestr withArguments:deletesArray];
        
            NSString *foo2=[insertsArray objectAtIndex:i];
            NSString* bar2 = [foo2 stringByReplacingOccurrencesOfString:@"'" withString:@"\""];
            NSString* insertstr = [bar2 stringByReplacingOccurrencesOfString:@"\\\"" withString:@"'"];
            [[DataBaseManager dataBaseManager]executeRecordsWithQuery:insertstr withArguments:deletesArray];
        }
      
        NSString *Deletependingquery=[NSString stringWithFormat:@"DELETE FROM pending_queries"];
      [[DataBaseManager dataBaseManager]executeRecordsWithQuery:Deletependingquery withArguments:deletesArray];
      responseStr=nil;
        
    }else if (connection==RegisterConnection){
        NSString *responseStr = [[NSString alloc] initWithBytes:[RegisterResponseData mutableBytes] length:[RegisterResponseData length] encoding:NSUTF8StringEncoding];
        
        NSError *error;
       
        dataDict=[[NSMutableDictionary alloc] init];
        NSData *jsonData = [responseStr dataUsingEncoding:NSUTF8StringEncoding];
        dataDict =[NSJSONSerialization JSONObjectWithData:jsonData options:0 error:&error];
        //////NSLog(@"register data %@",dataDict);
        
        NSString *Statusstr=[[dataDict objectForKey:@"response"] objectForKey:@"status"];
        
        UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"Success" message:Statusstr delegate:self cancelButtonTitle:@"ok" otherButtonTitles:nil, nil];
        [alert show];
        responseStr=nil;
        
        
    }else if (connection == loginConnection){
        
        NSString *responseStr;
        responseStr = [[NSString alloc] initWithBytes:[loginResponseData mutableBytes] length:[loginResponseData length] encoding:NSUTF8StringEncoding];
        
        NSError *error;
    
       dataDict=[[NSMutableDictionary alloc] init];
        NSData *jsonData = [responseStr dataUsingEncoding:NSUTF8StringEncoding];
        dataDict =[NSJSONSerialization JSONObjectWithData:jsonData options:0 error:&error];
        
        NSMutableArray *deleteArray=[dataDict objectForKey:@"delete-queries"];
        NSMutableArray *insertsArray=[dataDict objectForKey:@"insert-queries"];
        NSMutableArray *deletesArray;
        for (int i=0; i<[deleteArray count]; i++)
        {
            NSString *foo=[deleteArray objectAtIndex:i];
            NSString* bar = [foo stringByReplacingOccurrencesOfString:@"'" withString:@"\""];
            NSString* deletestr = [bar stringByReplacingOccurrencesOfString:@"\\\"" withString:@"'"];
            [[DataBaseManager dataBaseManager]executeRecordsWithQuery:deletestr withArguments:deletesArray];

            
            NSString *foo2=[insertsArray objectAtIndex:i];
            NSString* bar2 = [foo2 stringByReplacingOccurrencesOfString:@"'" withString:@"\""];
            NSString* insertstr = [bar2 stringByReplacingOccurrencesOfString:@"\\\"" withString:@"'"];
             [[DataBaseManager dataBaseManager]executeRecordsWithQuery:insertstr withArguments:deletesArray];
        }
      
        responseStr=nil;
    }
    
}
-(void)updateUI
{
    
    if (isLandScope){
       
    CGFloat width,height;
    width=1024;
    height=748;
    CGFloat startingposition=width/4;
   
    
    logobackview.frame=CGRectMake(0.0, y, width , (height/2));
   
    logoimageview.frame=CGRectMake(startingposition+60, y+20, logobackview.frame.size.width/3, (height/2)-80);
    
    backgroundcolorview.frame=CGRectMake(0,(height/2)+y,width,(height-((height/2))));
     
        HomeImageView.frame=CGRectMake(10,20,380,340);
    usernameleftview.frame=CGRectMake(startingposition+150,(backgroundcolorview.frame.size.height/3)-20, 70, 80);
    
        passwordleftview.frame=CGRectMake(startingposition+150,(backgroundcolorview.frame.size.height/3)+70, 70, 80);
   
      username_text.frame=CGRectMake(startingposition+usernameleftview.frame.size.width+150, (backgroundcolorview.frame.size.height/4)+10, 300, 80);
   
        
        password_text.frame=CGRectMake(startingposition+passwordleftview.frame.size.width+150, (backgroundcolorview.frame.size.height/4)+100, 300, 80);
    
        login_button.frame=CGRectMake(startingposition+passwordleftview.frame.size.width+username_text.frame.size.width-30+150, (backgroundcolorview.frame.size.height/4)+30, 130, 130);
   
       
   
        sationLbl.frame=CGRectMake(backgroundcolorview.frame.size.width-200, backgroundcolorview.frame.size.height-100,250, 30);
        
       
    
       Timelbl.frame=CGRectMake(backgroundcolorview.frame.size.width-250, backgroundcolorview.frame.size.height-50,250, 30);
   FileBtn.frame=CGRectMake(10,25,40,40);
   
 //File view
        
        FilebackView.frame=CGRectMake(10,20, 1004, 150);
        CGFloat Btnw=FilebackView.frame.size.width/4;
        RegisterBtn.frame=CGRectMake(60,25,Btnw,100);
        AboutBtn.frame=CGRectMake(60+RegisterBtn.frame.size.width+25,25,Btnw,100);
        ExitFileBtn.frame=CGRectMake(60+RegisterBtn.frame.size.width+20+AboutBtn.frame.size.width+30,25,Btnw,100);
   
 //About View
        
        Aboutpopup.frame=CGRectMake(0, 0, 1024, 768);
        AboutbackView.frame=CGRectMake(250, 20, 500, 748-40);
        AboutbackView.contentSize=CGSizeMake(500, 1000);
        logoImageView.frame=CGRectMake(10, 10,100 , 100);
        Titlelbl.frame=CGRectMake(logoImageView.frame.size.width+80,40,AboutbackView.frame.size.width-logoImageView.frame.size.width, 30);
        
        subtitlelbl.frame=CGRectMake(logoImageView.frame.size.width+60,80,AboutbackView.frame.size.width-logoImageView.frame.size.width, 30);
        
    
        Abouttextlbl.frame=CGRectMake(20,150,AboutbackView.frame.size.width-40,400);
    
        ViewLiceseBtn.frame=CGRectMake(20,450,AboutbackView.frame.size.width-40,40);
     
        ViewLicencetextview.frame=CGRectMake(20,500,AboutbackView.frame.size.width-40,200);
        
        OkAboutBtn.frame=CGRectMake(200,720,100,40);
 
        
//Register View\
      
        
        RegisterpopUp.frame=CGRectMake(0, 0, 1024, 768);

        RegisterView.frame=CGRectMake(250, 20, 500, 748-40);
        
        RegisterView.contentSize=CGSizeMake(500, 1000);
   
       // RegisterView.frame=CGRectMake(130,50, 500, 1004-300);
   
    ReglogoImageView.frame=CGRectMake(10, 10,100 , 100);
    
    RegTitlelbl.frame=CGRectMake(ReglogoImageView.frame.size.width+80,40,RegisterView.frame.size.width-ReglogoImageView.frame.size.width, 30);
    
    Regsubtitlelbl.frame=CGRectMake(ReglogoImageView.frame.size.width+60,80,RegisterView.frame.size.width-ReglogoImageView.frame.size.width, 30);
    
    
    CGFloat ReWidth=RegisterView.frame.size.width/2;
    Namelbl.frame=CGRectMake(10, 150,ReWidth-40, 40);
   
    NameField.frame=CGRectMake(Namelbl.frame.size.width, 150,ReWidth+30, 40);
  
    Emaillbl.frame=CGRectMake(10, 240,ReWidth-40, 40);
  
    EmailField.frame=CGRectMake(Namelbl.frame.size.width, 240,ReWidth+30, 40);
  
    PhNumberlbl.frame=CGRectMake(10, 330,ReWidth-40, 40);
  
    PhNumberField.frame=CGRectMake(Namelbl.frame.size.width, 330,ReWidth+30, 40);
   
    Companynamelbl.frame=CGRectMake(10, 420,ReWidth-40, 40);
   
    CompanynameField.frame=CGRectMake(Namelbl.frame.size.width, 420,ReWidth+30, 40);
 
    
    RegisterNowBtn.frame=CGRectMake(30,550,ReWidth-30,50);
    ReglaterBtn.frame=CGRectMake(ReWidth+10,550,ReWidth-30,50);

  
 //Licence view
        
        Licencepopup.frame=CGRectMake(0, 0, 1024, 768);
 
       // Licencepopup.frame=CGRectMake(0, 0, 768, 1024);
    
    LicenceView.frame=CGRectMake(20, 40, Licencepopup.frame.size.width-40, Licencepopup.frame.size.height-80);
  
    LicenceView.contentSize=CGSizeMake(Licencepopup.frame.size.width-40, Licencepopup.frame.size.height-20+300);
   
    LicenceWebView.frame=(CGRectMake(20, 10,LicenceView.frame.size.width-40, LicenceView.frame.size.height-100));

    
    CGFloat value=LicenceView.frame.size.width/3;

    AceeptBtn.frame=CGRectMake(value+30,LicenceView.frame.size.height-65,100,40);
  
    CancelBtn.frame=CGRectMake(value+180,LicenceView.frame.size.height-65,100,40);
    
        
    }else{
     
        CGFloat width,height;
        height=1004;
        width=768;
          CGFloat startingposition=width/4;
        logobackview.frame=CGRectMake(0.0, y,width , (height/4));
        
     logoimageview.frame=CGRectMake(startingposition+40, y+20, logobackview.frame.size.width/3,(logobackview.frame.size.width/3)-60);
        
        backgroundcolorview.frame=CGRectMake(0,(height/4)+y,width,(height-((height/4))));
        
       HomeImageView.frame=CGRectMake(startingposition,25,400,300);
        
        usernameleftview.frame=CGRectMake(startingposition,(backgroundcolorview.frame.size.height/3)+100, 70, 80);
        
      passwordleftview.frame=CGRectMake(startingposition,(backgroundcolorview.frame.size.height/3)+90+100, 70, 80);
        
        username_text.frame=CGRectMake(startingposition+usernameleftview.frame.size.width, (backgroundcolorview.frame.size.height/4)+65+100, 300, 80);
 
    password_text.frame=CGRectMake(startingposition+passwordleftview.frame.size.width, (backgroundcolorview.frame.size.height/4)+150+100, 300, 80);
        
       
    login_button.frame=CGRectMake(startingposition+passwordleftview.frame.size.width+username_text.frame.size.width-30, (backgroundcolorview.frame.size.height/4)+80+100, 130, 130);
       sationLbl.frame=CGRectMake(backgroundcolorview.frame.size.width-200, backgroundcolorview.frame.size.height-150,250, 30);
       
        Timelbl.frame=CGRectMake(backgroundcolorview.frame.size.width-250, backgroundcolorview.frame.size.height-100,250, 30);
        FileBtn.frame=CGRectMake(10,25,40,40);
        
 //File View
        
        FilebackView.frame=CGRectMake(10,20, 748, 150);
        CGFloat Btnw=FilebackView.frame.size.width/4;
        RegisterBtn.frame=CGRectMake(60,25,Btnw,100);
        AboutBtn.frame=CGRectMake(60+RegisterBtn.frame.size.width+25,25,Btnw,100);
        ExitFileBtn.frame=CGRectMake(60+RegisterBtn.frame.size.width+20+AboutBtn.frame.size.width+30,25,Btnw,100);
        
//About View
        
          Aboutpopup.frame=CGRectMake(0, 0, 768, 1024);
        AboutbackView.frame=CGRectMake(130,50, 500, 1004-200);
        logoImageView.frame=CGRectMake(10, 10,100 , 100);
    
        Titlelbl.frame=CGRectMake(logoImageView.frame.size.width+80,40,AboutbackView.frame.size.width-logoImageView.frame.size.width, 30);
        
        
        subtitlelbl.frame=CGRectMake(logoImageView.frame.size.width+60,80,AboutbackView.frame.size.width-logoImageView.frame.size.width, 30);
        
        
        Abouttextlbl.frame=CGRectMake(20,150,AboutbackView.frame.size.width-40,400);
        
        ViewLiceseBtn.frame=CGRectMake(20,450,AboutbackView.frame.size.width-40,40);
        
        ViewLicencetextview.frame=CGRectMake(20,500,AboutbackView.frame.size.width-40,200);
        
        OkAboutBtn.frame=CGRectMake(200,720,100,40);
        
        
//Register View
            RegisterpopUp.frame=CGRectMake(0, 0, 768, 1024);
        RegisterView.frame=CGRectMake(130,50, 500, 1004-300);
        
        ReglogoImageView.frame=CGRectMake(10, 10,100 , 100);
        
        RegTitlelbl.frame=CGRectMake(ReglogoImageView.frame.size.width+80,40,RegisterView.frame.size.width-ReglogoImageView.frame.size.width, 30);
        
        Regsubtitlelbl.frame=CGRectMake(ReglogoImageView.frame.size.width+60,80,RegisterView.frame.size.width-ReglogoImageView.frame.size.width, 30);
        
        
        CGFloat ReWidth=RegisterView.frame.size.width/2;
        Namelbl.frame=CGRectMake(10, 150,ReWidth-40, 40);
        
        NameField.frame=CGRectMake(Namelbl.frame.size.width, 150,ReWidth+30, 40);
        
        Emaillbl.frame=CGRectMake(10, 240,ReWidth-40, 40);
        
        EmailField.frame=CGRectMake(Namelbl.frame.size.width, 240,ReWidth+30, 40);
        
        PhNumberlbl.frame=CGRectMake(10, 330,ReWidth-40, 40);
        
        PhNumberField.frame=CGRectMake(Namelbl.frame.size.width, 330,ReWidth+30, 40);
        
        Companynamelbl.frame=CGRectMake(10, 420,ReWidth-40, 40);
        
        CompanynameField.frame=CGRectMake(Namelbl.frame.size.width, 420,ReWidth+30, 40);
        
        
        RegisterNowBtn.frame=CGRectMake(30,550,ReWidth-30,50);
        ReglaterBtn.frame=CGRectMake(ReWidth+10,550,ReWidth-30,50);
     
        
//Licence View
       Licencepopup.frame=CGRectMake(0, 0, 768, 1024);
        
        LicenceView.frame=CGRectMake(20, 40, Licencepopup.frame.size.width-40, Licencepopup.frame.size.height-80);
        
        LicenceView.contentSize=CGSizeMake(Licencepopup.frame.size.width-40, Licencepopup.frame.size.height-20+300);
        
        LicenceWebView.frame=(CGRectMake(20, 10,LicenceView.frame.size.width-40, LicenceView.frame.size.height-100));
        
        
        CGFloat value=LicenceView.frame.size.width/3;
        
        AceeptBtn.frame=CGRectMake(value+30,LicenceView.frame.size.height-65,100,40);
        
        CancelBtn.frame=CGRectMake(value+180,LicenceView.frame.size.height-65,100,40);
        
    }
    
}
-(void)releaseViews
{
    
    NSArray *mainArray = self.view.subviews;
    for (int i = 0; i<[mainArray count]; i++) {
        UIView *vi =[mainArray objectAtIndex:i];
        [vi removeFromSuperview];
        vi = nil;
    }
    
    NSArray *backArray = backgroundcolorview.subviews;
    for (int i = 0; i<[backArray count]; i++) {
        UIView *vi =[backArray objectAtIndex:i];
        [vi removeFromSuperview];
        vi = nil;
    }
    NSArray *fileArray = filepopupView.subviews;
    for (int i = 0; i<[fileArray count]; i++) {
        UIView *vi =[fileArray objectAtIndex:i];
        [vi removeFromSuperview];
        vi = nil;
    }
    NSArray *aboutArray = Aboutpopup.subviews;
    for (int i = 0; i<[aboutArray count]; i++) {
        UIView *vi =[aboutArray objectAtIndex:i];
        [vi removeFromSuperview];
        vi = nil;
    }
    NSArray *RegisterArray = RegisterpopUp.subviews;
    for (int i = 0; i<[RegisterArray count]; i++) {
        UIView *vi =[RegisterArray objectAtIndex:i];
        [vi removeFromSuperview];
        vi = nil;
    }
    NSArray *LicenceArray = Licencepopup.subviews;
    for (int i = 0; i<[LicenceArray count]; i++) {
        UIView *vi =[LicenceArray objectAtIndex:i];
        [vi removeFromSuperview];
        vi = nil;
    }

    [backgroundcolorview removeFromSuperview];
   [logoimageview removeFromSuperview],[logobackview removeFromSuperview],[passwordleftview removeFromSuperview],[usernameleftview removeFromSuperview];
    logoimageview=nil,logobackview=nil,passwordleftview=nil,usernameleftview=nil;
    backgroundcolorview=nil;
    username_text=nil,password_text=nil;
    login_button=nil;
    
    dataBasePath=nil;
    username=nil,password=nil;
     connection1=nil,PendimgConnection=nil,RegisterConnection=nil,loginConnection=nil;
     responseData=nil,PendingResponseData=nil,RegisterResponseData=nil,loginResponseData=nil;
     dataDict=nil;
    [HomeImageView removeFromSuperview];
    HomeImageView=nil;
    sationLbl=nil,Timelbl=nil;
    FileBtn=nil;
    
    //file view
  
    NSArray *FilebackViewArray = FilebackView.subviews;
    for (int i = 0; i<[FilebackViewArray count]; i++) {
        UIView *vi =[FilebackViewArray objectAtIndex:i];
        [vi removeFromSuperview];
        vi = nil;
    }
    NSArray *filepopupViewArray = filepopupView.subviews;
    for (int i = 0; i<[filepopupViewArray count]; i++) {
        UIView *vi =[filepopupViewArray objectAtIndex:i];
        [vi removeFromSuperview];
        vi = nil;
    }
    [filepopupView removeFromSuperview],[FilebackView removeFromSuperview];[ExitFileBtn removeFromSuperview],[AboutBtn removeFromSuperview],[RegisterBtn removeFromSuperview];
    filepopupView=nil,FilebackView=nil;
    ExitFileBtn=nil,AboutBtn=nil,RegisterBtn=nil;
    
    //about view
  
    NSArray *AboutbackViewArray = AboutbackView.subviews;
    for (int i = 0; i<[AboutbackViewArray count]; i++) {
        UIView *vi =[AboutbackViewArray objectAtIndex:i];
        [vi removeFromSuperview];
        vi = nil;
    }
    NSArray *AboutpopupArray = Aboutpopup.subviews;
    for (int i = 0; i<[AboutpopupArray count]; i++) {
        UIView *vi =[AboutpopupArray objectAtIndex:i];
        [vi removeFromSuperview];
        vi = nil;
    }
   
    [Aboutpopup removeFromSuperview],[AboutbackView removeFromSuperview],[logoImageView removeFromSuperview],[ViewLicencetextview removeFromSuperview],[ViewLiceseBtn removeFromSuperview],[Timelbl removeFromSuperview],[Titlelbl removeFromSuperview],[subtitlelbl removeFromSuperview],[Abouttextlbl removeFromSuperview],[OkAboutBtn removeFromSuperview];
    Aboutpopup=nil;
    AboutbackView=nil;
    logoImageView=nil;
    ViewLiceseBtn=nil;
    Titlelbl=nil,subtitlelbl=nil;
     Abouttextlbl=nil,ViewLicencetextview=nil;
    OkAboutBtn=nil;
    
    //Register View
   
    NSArray *RegisterpopUpArray = RegisterpopUp.subviews;
    for (int i = 0; i<[RegisterpopUpArray count]; i++) {
        UIView *vi =[RegisterpopUpArray objectAtIndex:i];
        [vi removeFromSuperview];
        vi = nil;
    }
    [RegisterpopUp removeFromSuperview],[RegisterBtn removeFromSuperview],[ReglogoImageView removeFromSuperview],[RegTitlelbl removeFromSuperview],[Regsubtitlelbl removeFromSuperview],[RegisterNowBtn removeFromSuperview],[ReglaterBtn removeFromSuperview];
    RegisterpopUp=nil;
    RegisterView=nil;
    ReglogoImageView=nil;
    RegTitlelbl=nil,Regsubtitlelbl=nil;
    RegisterNowBtn=nil,ReglaterBtn=nil;
    
    Namelbl=nil,Emaillbl=nil,PhNumberlbl=nil,Companynamelbl=nil;
    NameField=nil,EmailField=nil,PhNumberField=nil,CompanynameField=nil;
    
    //Licence View
    NSArray *LicenceViewArray = LicenceView.subviews;
    for (int i = 0; i<[LicenceViewArray count]; i++) {
        UIView *vi =[LicenceViewArray objectAtIndex:i];
        [vi removeFromSuperview];
        vi = nil;
    }
    NSArray *LicencepopupArray = Licencepopup.subviews;
    for (int i = 0; i<[LicencepopupArray count]; i++) {
        UIView *vi =[LicencepopupArray objectAtIndex:i];
        [vi removeFromSuperview];
        vi = nil;
    }
      [LicenceWebView loadRequest:nil];
   [Licencepopup removeFromSuperview],[LicenceView removeFromSuperview], [LicenceWebView removeFromSuperview],[AceeptBtn removeFromSuperview],[CancelBtn removeFromSuperview];
    Licencepopup=nil;
    LicenceView=nil;
    LicenceWebView=nil;
    AceeptBtn=nil,CancelBtn=nil;
    
    [self.view removeFromSuperview];
    self.view=nil;
    
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
  
    NSArray *FilebackViewArray = FilebackView.subviews;
    for (int i = 0; i<[FilebackViewArray count]; i++) {
        UIView *vi =[FilebackViewArray objectAtIndex:i];
        [vi removeFromSuperview];
        vi = nil;
    }
    NSArray *filepopupViewArray = filepopupView.subviews;
    for (int i = 0; i<[filepopupViewArray count]; i++) {
        UIView *vi =[filepopupViewArray objectAtIndex:i];
        [vi removeFromSuperview];
        vi = nil;
    }
    [filepopupView removeFromSuperview],[FilebackView removeFromSuperview];
    [FilebackView removeFromSuperview];
    NSArray *AboutbackViewArray = AboutbackView.subviews;
    for (int i = 0; i<[AboutbackViewArray count]; i++) {
        UIView *vi =[AboutbackViewArray objectAtIndex:i];
        [vi removeFromSuperview];
        vi = nil;
    }
    NSArray *AboutpopupArray = Aboutpopup.subviews;
    for (int i = 0; i<[AboutpopupArray count]; i++) {
        UIView *vi =[AboutpopupArray objectAtIndex:i];
        [vi removeFromSuperview];
        vi = nil;
    }
    
    [Aboutpopup removeFromSuperview],[AboutbackView removeFromSuperview];
    [Aboutpopup removeFromSuperview];
    NSArray *RegisterpopUpArray = RegisterpopUp.subviews;
    for (int i = 0; i<[RegisterpopUpArray count]; i++) {
        UIView *vi =[RegisterpopUpArray objectAtIndex:i];
        [vi removeFromSuperview];
        vi = nil;
    }
    [RegisterpopUp removeFromSuperview];
     [RegisterpopUp removeFromSuperview];
    
}
- (BOOL)shouldAutorotate
{
     ////NSLog(@"called");
   
    
    return NO;


}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return YES;
}

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration

{
    if (toInterfaceOrientation==UIInterfaceOrientationLandscapeRight||toInterfaceOrientation==UIInterfaceOrientationLandscapeLeft) {
        isLandScope=YES;
        
    }else
    {
        isLandScope=NO;
    }
    [self updateUI];
    
}

- (NSUInteger)supportedInterfaceOrientations // iOS 6 autorotation fix
{
    return UIInterfaceOrientationMaskLandscape;
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation // iOS 6 autorotation fix
{
    return UIInterfaceOrientationPortrait;
}

-(void)textFieldDidBeginEditing:(UITextField *)textField
{   if (textField==username_text||password_text)
   {
      if (isLandScope)
      {
       // ////NSLog(@" y value %f",backgroundcolorview.frame.origin.y);
        //[UIView beginAnimations:nil context:NULL];
       // [UIView setAnimationDuration:0.0f];
        logobackview.transform = CGAffineTransformMakeTranslation(0,-logobackview.frame.size.height);
        
      //  [UIView commitAnimations];
        
        
      //  [UIView beginAnimations:nil context:NULL];
        //[UIView setAnimationDuration:0.0f];
        
        backgroundcolorview.transform = CGAffineTransformMakeTranslation(0,-logobackview.frame.size.height);
        
      //  [UIView commitAnimations];
        
        //////NSLog(@"%f",backgroundcolorview.frame.origin.y);
      }
   }
    
}
-(void)textFieldDidEndEditing:(UITextField *)textField
{
    if (textField==username_text||password_text)
    {
        if (isLandScope)
        {
           // ////NSLog(@"called");
            
          //
           // [UIView beginAnimations:nil context:NULL];
//[UIView setAnimationDuration:0.0f];
            logobackview.transform = CGAffineTransformMakeTranslation(0,0);
            
          //  [UIView commitAnimations];
            
            
          //  [UIView beginAnimations:nil context:NULL];
          //  [UIView setAnimationDuration:0.0f];
            
            backgroundcolorview.transform = CGAffineTransformMakeTranslation(0,0);
          //  [UIView commitAnimations];
            ////NSLog(@"%f",backgroundcolorview.frame.origin.y);
        }
    }
    

    
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
